package com.syher.quartz.task.job;

import com.syher.quartz.common.ConstantValue;
import com.syher.quartz.core.config.quartz.job.SyncJob;
import com.syher.quartz.task.model.TaskInfo;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.UnableToInterruptJobException;

import java.util.Date;

/**
 * Created by shenyuhang on 2018/3/7.
 */
public class TaskJob extends SyncJob {
    TaskInfo task;

    @Override
    public void doExecute(JobExecutionContext context) throws JobExecutionException {
        task = (TaskInfo) context.getJobDetail()
                .getJobDataMap().get(ConstantValue.DEFAULT_TASK_JOB);

        Date now = new Date();
        int i = 0;
        while (!currentThread.isInterrupted()) {
            if (i >= 11) break;
            Date current = new Date();
            if ((current.getTime() - now.getTime()) / 1000 > 1) {
                System.out.println(task.getName() + "\texecute count:" +(i + 1));
                now = current;
                i++;
            }
        }
    }

    @Override
    public void interrupt() throws UnableToInterruptJobException {
        if (currentThread != null) {
            System.out.println("task interrupted." + task.getName());
            currentThread.interrupt();
        }
    }
}
